import axios from "axios";
import { session } from '@/service/storage.service'
import { CamelToSnake, SnakeToCamel } from '@multi-kit/convention';
import { formatObjectKeys } from '@/utils/convention'

const { VITE_USER_TOKEN, VITE_API_URL } = import.meta.env;


// axios 请求方式枚举
const METHOD = {
  GET: "GET",
  DELETE: "DELETE",
  HEAD: "HEAD",
  OPTIONS: "OPTIONS",
  POST: "POST",
  PUT: "PUT",
  PATCH: "PATCH",
  PURGE: "PURGE",
  LINK: "LINK",
  UNLINK: "UNLINK",
};
export { METHOD };


/**
 * 构建axios实例并增加拦截器
 * @author WangLiZhi
 * @param {string} baseURL
 * @param {object} config
 * @returns {import("axios").AxiosInstance}
 */
function create(baseURL = "", config = {}) {
  // 初始化axios实例
  const http = axios.create({
    timeout: 30000,
    baseURL: baseURL,
    headers: {
      "Content-Type": "application/json;charset=UTF-8;",
    },
    ...config,
  });

  // 请求拦截器
  http.interceptors.request.use(
    (config) => {
      
      // http header上添加token
      const token = session.get(VITE_USER_TOKEN);
   
      if (token) {
        config.headers.authorization = token;
      }

      const method = config.method.toUpperCase();
      if (method === METHOD.GET || method === METHOD.DELETE) {
        config.params = formatObjectKeys(config.params, CamelToSnake) || {};
      }
      if (method === METHOD.POST || method === METHOD.PUT) {
        config.data = formatObjectKeys(config.data, CamelToSnake) || {};
      }

      return config;
    },
    (error) => Promise.reject(error)
  );

  // 响应拦截白名单
  const whiteList = [200, 201, 202, 204];

  // 响应拦截器
  http.interceptors.response.use(
    (response) => {
      
      if (whiteList.includes(response.status)) {
        
        response.data = formatObjectKeys(response.data, SnakeToCamel, 2)
        console.log("http.interceptors.response", response.data)
        return Promise.resolve(response);
      }
      return Promise.reject(response);
    },
    (error) => {
      const status = error?.response?.status;
      
      if (status) {
        let message;
        switch (status) {
          case 400:
            message = "请求错误!";
            break;
          case 401:
            message = "请求错误!";
            session.remove(VITE_USER_TOKEN);
            break;
          case 404:
            message = "此刻无数据!";
            break;
          case 408:
            message = "请求超时!";
            break;
          case 500:
            message = "服务器内部错误!";
            break;
          case 501:
            message = "服务未实现!";
            break;
          case 502:
            message = "网关错误!";
            break;
          case 503:
            message = "服务不可用!";
            break;
          case 504:
            message = "网关超时!";
            break;
          case 505:
            message = "HTTP版本不受支持!";
            break;
          default:
            message = "请求失败!";
        }
      }
      return Promise.reject(error);
    }
  );

  return http;
}

// 创建请求接口的axios实例
export const request = create(VITE_API_URL);
export const fileRequest = create(VITE_API_URL, {
  headers: {
    'Content-Type': 'multipart/form-data', // 可以手动指定，也可以不写
  }
});



